package com.sline.test;

import java.io.*;

public class DirectoryCopy {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入需要拷贝的文件夹");
        File src = DirectorySize.getFile();
        System.out.println("请输入拷贝至目录");
        File desc = DirectorySize.getFile();

        if (!src.equals(desc)) {
            copy(src, desc);
        }
    }

    // 将src文件夹完整的拷贝到desc目录下
    public static void copy(File src, File desc) throws IOException {
        File newDir = new File(desc, src.getName());
        newDir.mkdir();

        File[] subfiles = src.listFiles();
        for (File subfile: subfiles) {
            if (subfile.isFile()) {
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream(subfile));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(new File(newDir, subfile.getName())));
                int b;
                while ((b = fis.read()) != -1) {
                    fos.write(b);
                }
                fis.close();
                fos.close();
            } else if (subfile.isDirectory()) {
                copy(subfile, newDir);
            }
        }
    }
}
